package util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;

/**
 * xml操作
 * @author DengZhiYuan
 * @version 1.0
 * @date 2023/2/23 11:23
 */
public class JaxbXmlBeanUtils {

    /**
     * xml转java对象
     * @author DengZhiYuan
     * @version 1.0
     * @date 2023/3/11 18:43
     * @param xml
     * @param className
     * @return
     */
    public static Object xml2Bean(String xml, Class className) {
        StringReader sr = new StringReader(xml);
        try {
            JAXBContext context = JAXBContext.newInstance(new Class[]{className});
            Unmarshaller um = context.createUnmarshaller();
            return um.unmarshal(sr);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }
}
